package MawsonClient.GUI;

import MawsonClient.LogHandler;
import org.freixas.jcalendar.JCalendarCombo;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Vector;

/**
 * User: Ryan Green
 * Date: Nov 5, 2010
 * Time: 4:20:24 PM
 * Copyright - University of South Australia
 */
public class RequestedDataElement {
	Component comp;
	String label;
	String defaultValue = "";
	Vector<String> comboElements;
	boolean required;
	int textLength;

	public int aggIndex = -1;

	LogHandler log;

	private String val;

	public RequestedDataElement(String label, Component comp, int aggIndex, int textLength, Vector<String> comboElements, String defaultValue, boolean required, LogHandler log){
		this.aggIndex = aggIndex;
		this.label = label;
		this.comp = comp;
		this.textLength = textLength;
		this.comboElements = comboElements;
		this.defaultValue = defaultValue;
		this.required = required;
		this.log = log;
	}

	public RequestedDataElement getOrigComponent() {
		if (comp instanceof JTextField) {
			return new RequestedDataElement(label, new MawsonTextField(defaultValue, required, new JTextFieldLimit(textLength)), aggIndex, textLength, null, defaultValue, required, log);
		} else if (comp instanceof JCalendarCombo) {
			JCalendarCombo jcal = new JCalendarCombo();
			jcal.setDateFormat(new SimpleDateFormat("dd MMM yyyy"));
			return new RequestedDataElement(label, jcal, aggIndex, textLength, null, defaultValue, required, log);
		} else if (comp instanceof JComboBox) {
			return new RequestedDataElement(label, new JComboBox(comboElements), aggIndex, textLength, comboElements, defaultValue, required, log);
		} else {
			log.LogAndExit("Unknown component type in config file - " + comp + " . Now exiting", true);
		}		
		return null;
	}

	public void setVal(String val){
		this.val = val;
	}

	public String getVal(){
		return val;
	}
}
